﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Googler.BLL;
using GSearch;

namespace HodhodNews.Aggregation
{
    public class GoogleJsonNewsAggregationProvider : NewsAggregationProvider
    {
        public override List<News> Aggregate(AggregationOptions options)
        {
            List<News> list = new List<News>();

            var search = new GNewsSearch();
            var response = search.Search(new NewsSearchArgs()
             {
                 ApiKey = "ABQIAAAArhaTt7E9J0aEH3HOI0LADxTT48ELYXW9lG0vh8zTTLgIL9G1ghSxHI3uA9KTGg4w1ROv3H8P-NMtDw",
                 Edition = SearchNewsEdition.Arabic,
                 ResultSize = SearchResultSize.Large,
                 Topic = SearchNewsTopics.All,
                 HostLang = SearchHostLang.Arabic,
                 Terms = string.Join(" ", options.Keywords.Select(k => k.Name).ToArray()), 

             });

            var results = response.Response.Results;
            if (results != null)
            {
                foreach (var r in results)
                {
                    var news = new News();
                    news.SummaryHtml = r.Content;
                    news.EntryId = r.ClusterUrl;
                    news.Link = r.UnescapedUrl;
                    news.PublishDate = Convert.ToDateTime(r.PublishedDate);
                    news.ModificationDate = news.PublishDate;
                    news.Title = r.TitleNoFormatting;
                    list.Add(news);
                }

            }
            return list;

        }
    }
}
